import axiosInstance  from "../../utils/request";
import {
    getPreDataDto,
    getNextData,
    CommodityDetail,
    CommonResult,
    SimilarityItem, CommodityReviewItem, CommodityDetailDto, LicenseEntity, TagEntity, BizTable, LicenseDetailDto
} from "../../components/MasterModal/plugins/dto";
import {AxiosRequestConfig, AxiosResponse} from "axios";
import {
    BrandResponseDto,
    CommonPageRequest,
    CommonPageResponse, ParamDto, RegistritionResponseDto,
    UnitResponseDto
} from "../../utils/types"
import {BusinessType, BusinessTypeEnum, CommodityAuditRequestDto} from "../../components/MasterModal/type";
import {ClassifyTreeDto} from "../../components/MasterModal/lib/Base/CommodityClassify/type";


// 获取商品详情
const getCommodityUrl = "/api/admin/commodity/apply/detail"
// 获取品牌详情
const getBrandDetailUrl = "/api/admin/brand/apply/detail"
// 获取品牌主数据详情
const getBrandMasterDetailUrl = "/api/admin/brand/md/detail"

// 获取商品证照详情
const getCommodityLicenseUrl = "/api/admin/License/editCommodityLicense"
// 获取商品证照主数据详情
const getCommodityLicenseMasterUrl = "/api/admin/License/getMdCommodityLicenseDetail"
// 获取合作商证照主数据详情
const getPartnerLicenseMasterUrl = "/api/admin/partnerLicense/getMdPartnerLicenseDetail"

// 获取合作商证照详情
const getPartnerLicenseUrl = "/api/admin/partnerLicense/editPartnerLicense"
// 审批商品
const auditCommodityUrl = "/api/admin/commodity/review/apply"
// 审批品牌
const auditBrandUrl = "api/admin/brand/review/apply"
// 审批商品证照
const auditCommodityLicenseUrl = "/api/admin/License/approvalLicense"
// 审批匹配商品证照
const auditCommodityLicenseMatchUrl = "/api/admin/License/approvalMatchLicense"
// 审批合作商证照
const auditPartnerLicenseUrl = "/api/admin/partnerLicense/approvalPartnerLicense"
// 审批合作商匹配证照
const auditPartnerLicenseMatchUrl = "/api/admin/partnerLicense/approvalMatchPartnerLicense"
// 更新商品信息
const  updateCommodityUrl = "/api/admin/commodity/md/update"
// 获取匹配商品相似度列表
const getSimilarityList = "/api/admin/commodity/match/similarity/list"
// 获取匹配品牌相似度列表
const getBrandSimilarityList = "/api/admin/brand/match/similarity/list"
// 获取匹配合作商证照相似度列表
const getPartnerLicenseSimilarityList = "/api/admin/partnerLicense/matchPartnerLicenseList"
// 获取匹配商品证照相似度列表
const getCommodityLicenseSimilarityList = "/api/admin/License/matchLicenseList"
// 计算相似度
const computeSimilarityList = "/api/admin/commodity/md/merge/similarity"
// 合并商品主数据
const mergeCommodityUrl = "/api/admin/commodity/md/merge/submit"
// 待审批商品列表
const reviewListUrl = "/api/admin/commodity/review/next"
// 待审批品牌下一条
const reviewBrandListUrl = "/api/admin/brand/review/next"
// 待审批合作商证照下一条
const reviewPartnerLicenseListUrl = "/api/admin/partnerLicense/review/next"
// 待审批商品证照下一条
const reviewCommodityListUrl = "/api/admin/License/review/next"




// 待匹配商品
const matchListUrl = "/api/admin/commodity/match/next"
// 待匹配品牌
const matchBrandListUrl = "/api/admin/brand/match/next"
// 待匹配合作商证照
const matchPartnerLicenseListUrl = "/api/admin/partnerLicense/match/next"
// 待匹配商品证照
const matchCommodityListUrl = "/api/admin/License/match/next"
// 获取商品证照列表
const getRegistrationUrl = "/api/admin/commodity/md/license/select"

// 获取商品分类列表
const getClassifyUrl = "/api/admin/commodity/class/treeList"
// 品牌主数据列表
const brandMdUrl = "/api/admin/brand/md/list"
// 商品单位
const unitMdUrl = "/api/admin/commodity/unit/page/list"
// 获取字典数据
const getParamsData = "/api/admin/sys/getParamsByList"

// 获取标签列表
const getTagList = "/api/admin/tag/list"


// 待匹配商品数据审批
const auditCommodityMatch = "/api/admin/commodity/match/apply"

// 待匹配品牌数据审批
const auditBrandMatch = "/api/admin/brand/match/apply"

//获取品牌主数据详情
const getMasterBrandInfo = "/api/admin/brand/md/detail"

//获取注册证主数据详情
const getMasterLicenseInfo = "/api/admin/License/getMdCommodityLicenseDetail"

//获取消毒证主数据详情
const getMasterPartnerLicenseInfo = "/api/admin/partnerLicense/getMdPartnerLicenseDetail"





const getRegistrationDetailUrl = "/api/admin/License/editCommodityLicense"


const generateBrandCodeUrl = "/api/admin/brand/review/generateCode"
export default {
    getPrePage: (obj: getPreDataDto) => {
        const params: AxiosRequestConfig = {
            params: obj
        }
        return axiosInstance.get("", params);
    },
    getNextPage: (obj: getNextData) => {
        const params: AxiosRequestConfig = {
            params: obj
        }
        return axiosInstance.get("", params);
    },
    getCommodityDetail: (id:string, businessType:BusinessTypeEnum): Promise<CommonResult<CommodityDetailDto|LicenseDetailDto>> => {
        return new Promise((resolve, reject) => {
            const url = businessType === 'commodity' ?
                getCommodityUrl : businessType === 'brand' ?
                getBrandDetailUrl : ["commodityLicense", "foreignLicense" , "domesticRecord" , "foreignRecord"].includes(businessType) ?
                getCommodityLicenseUrl : businessType === "partnerLicense" ?
                getPartnerLicenseUrl : ""
            axiosInstance.get<CommonResult<CommodityDetailDto>>(url, {params: {id}}).then((response: AxiosResponse) => {
                resolve(response.data as CommonResult<CommodityDetailDto>)
            });
        })
    },
    getCommodityMasterDetail: (id:string, businessType:BusinessTypeEnum): Promise<CommonResult<CommodityDetailDto|LicenseDetailDto>> => {
        return new Promise((resolve, reject) => {
            const url = businessType === 'commodity' ?
                getCommodityUrl : businessType === 'brand' ?
                getBrandMasterDetailUrl : ["commodityLicense", "foreignLicense" , "domesticRecord" , "foreignRecord"].includes(businessType) ?
                getCommodityLicenseMasterUrl : businessType === "partnerLicense" ?
                getPartnerLicenseMasterUrl : ""
            axiosInstance.get<CommonResult<CommodityDetailDto>>(url, {params: {id}}).then((response: AxiosResponse) => {
                resolve(response.data as CommonResult<CommodityDetailDto>)
            });
        })
    },
    updateCommodityInfo: (data: CommodityDetail) => {
        return axiosInstance.post<CommonResult<object>>(getCommodityUrl, data);
    },
    auditCommodity: (data: CommodityAuditRequestDto, businessType:BusinessTypeEnum):Promise<CommonResult<any>> => {
        return new Promise((resolve, reject)=>{
            const url = businessType === 'commodity' ?
                auditCommodityUrl : businessType === 'brand' ?
                    auditBrandUrl : ["commodityLicense", "foreignLicense" , "domesticRecord" , "foreignRecord"].includes(businessType) ?
                        auditCommodityLicenseUrl : businessType === "partnerLicense" ?
                            auditPartnerLicenseUrl : ""
            axiosInstance.post<CommonResult<any>>(url, data).then((response: AxiosResponse) => {
                resolve(response.data)
            })
        })
    },
    getSimilarityList: (data: {id: string, brandStandardCode?:string, keyword?:string, licenseStandarCode?:string}, businessType:BusinessTypeEnum):Promise<CommonResult<SimilarityItem[]>> => {
        return new Promise((resolve)=>{
            const url = businessType === 'commodity' ?
                getSimilarityList : businessType === 'brand' ?
                    getBrandSimilarityList : ["commodityLicense", "foreignLicense" , "domesticRecord" , "foreignRecord"].includes(businessType) ?
                        getCommodityLicenseSimilarityList : businessType === "partnerLicense" ?
                            getPartnerLicenseSimilarityList : ""
            axiosInstance.post<CommonResult<SimilarityItem[]>>(url, data).then((response:AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    computeSimilarityList: (data:{fetches:string[], id:number, matchField:object}) => {
        return axiosInstance.post<CommonResult<object>>(computeSimilarityList, data)
    },
    mergeCommodityData: (data:{idList:string[], masterId:number}) => {
        return axiosInstance.post<CommonResult<object>>(mergeCommodityUrl, data)
    },
    getReviewList: (data:any, businessType:BusinessTypeEnum):Promise<CommonResult<{id:number}>> => {
        return new Promise((resolve, reject) => {
            const url = businessType === 'commodity' ?
                reviewListUrl : businessType === 'brand' ?
                    reviewBrandListUrl : ["commodityLicense", "foreignLicense" , "domesticRecord" , "foreignRecord"].includes(businessType) ?
                        reviewCommodityListUrl : businessType === "partnerLicense" ?
                            reviewPartnerLicenseListUrl : ""
            console.log(232323,url)
                axiosInstance.get<CommonResult<{id:number}>>(url, {params: data}).then((response: AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    getMatchList: (data:any, businessType:BusinessTypeEnum):Promise<CommonResult<{ id:number }>> => {
        return new Promise((resolve, reject) => {
            const url = businessType === 'commodity' ?
                matchListUrl : businessType === 'brand' ?
                    matchBrandListUrl : ["commodityLicense", "foreignLicense" , "domesticRecord" , "foreignRecord"].includes(businessType) ?
                        matchCommodityListUrl : businessType === "partnerLicense" ?
                            matchPartnerLicenseListUrl : ""
            axiosInstance.get<CommonResult<{id:number}>>(url, {params: data}).then((response: AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    getClassifyData:():Promise<CommonResult<ClassifyTreeDto[]>> => {
        return new Promise((resolve, reject)=>{
            axiosInstance.get<CommonResult<ClassifyTreeDto[]>>(getClassifyUrl, {}).then((response:AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    getBrandData:(data:any):Promise<CommonPageResponse<BrandResponseDto>> => {
        return new Promise((resolve, reject)=>{
            axiosInstance.get<CommonPageResponse<BrandResponseDto>>(brandMdUrl, {params:data}).then((response:AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    getUnitPageData:(data:any):Promise<CommonPageResponse<UnitResponseDto>> => {
        return new Promise((resolve, reject)=>{
            axiosInstance.get<CommonPageResponse<UnitResponseDto>>(unitMdUrl, {params:data}).then((response:AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    getParamsData:(data:{strList: string[]}):Promise<CommonResult<ParamDto[]>> =>{
        return new Promise((resolve, reject)=>{
            axiosInstance.post(getParamsData, {data}).then((response:AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    getLicenseDetail:(data: {id:string}):Promise<CommonResult<RegistritionResponseDto>> =>{
        return new Promise((resolve, reject)=>{
            axiosInstance.post(getRegistrationDetailUrl, data).then((response:AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    getLicenseList:(data:{currentPage:number,pageSize:number, keyword:string}):Promise<CommonPageResponse<LicenseEntity>> =>{
        return new Promise((resolve, reject)=>{
            axiosInstance.get(getRegistrationUrl, {params:data}).then((response:AxiosResponse)=>{
                resolve(response.data)
            })
        })
    },
    getTagList: (data:{bizTable:BizTable}):Promise<CommonResult<TagEntity[]>> =>{
        return new Promise((resolve, reject)=>{
            axiosInstance.get(getTagList, {params:data}).then((response)=>{
                resolve(response.data)
            })
        })
    },
    auditMatchCommodity: (data:any, businessType:BusinessTypeEnum):Promise<CommonResult<any>> => {
        return new Promise((resolve, reject) => {
            const url = businessType === 'commodity' ?
                auditCommodityMatch : businessType === 'brand' ?
                    auditBrandMatch : ["commodityLicense", "foreignLicense" , "domesticRecord" , "foreignRecord"].includes(businessType) ?
                        auditCommodityLicenseMatchUrl : businessType === "partnerLicense" ?
                            auditPartnerLicenseMatchUrl : ""
            axiosInstance.post(url, data).then((response)=>{
                resolve(response.data)
            })
        })
    },
    generateCode: (data:any):Promise<CommonResult<string>> =>{
        return new Promise((resolve, reject)=>{
            axiosInstance.get(generateBrandCodeUrl, {params: {...data}}).then((response)=>{
                resolve(response.data)
            })
        })
    },

    getMasterInfo: (id:string, businessType:BusinessTypeEnum):Promise<CommonResult<any>> => {
        return new Promise((resolve, reject) => {
            const url = businessType === 'commodity' ?
                '' : businessType === 'brand' ?
                getMasterBrandInfo : ["commodityLicense", "foreignLicense" , "domesticRecord" , "foreignRecord"].includes(businessType) ?
                getMasterLicenseInfo : businessType === "partnerLicense" ?
                getMasterPartnerLicenseInfo : ""
            axiosInstance.get<CommonResult<CommodityDetailDto>>(url, {params: {id}}).then((response: AxiosResponse) => {
                resolve(response.data as CommonResult<CommodityDetailDto>)
            });
        })
    },
}